<template>
        <div class="list" ref="wrapper">
            <div>
                <div class="area" ref="currentCity">
                  <div class="title border-topbottom">当前城市</div>
                  <div class="buttonList">
                    <div class="button-wrapper">
                        <div class="button">{{this.city}}</div>
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="buttonList">
                    <div class="button-wrapper" v-for="item in hotCitiesList" :key="item.id">
                        <div class="button" @click="change(item.name)">{{item.name}}</div>
                    </div>
                </div>
            </div>
            <div class="area" v-for="key in keys" :key="key" :ref="key">
                <div class="title border-topbottom">{{key}}</div>
                <div class="item border-bottom" v-for="item in cities[key]" :key="item.id" @click="change(item.name)">{{item.name}}</div>
            </div>
            </div>
        </div>
</template>
<script>
import BScroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    'hotCitiesList': {
      type: Array
    },
    'cities': {
      type: Object
    },
    'letter': {
      type: String
    }
  },
  data () {
    return {
      scroll: null
    }
  },
  computed: {
    keys () {
      return Object.keys(this.cities)
    },
    ...mapState(['city'])
  },
  methods: {
    goTop () {
      let el = this.$refs.currentCity
      this.scroll.scrollToElement(el)
    },
    change (data) {
      this.changeCity(data)
      this.goTop()
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  watch: {
    letter: function (newVal, oldVal) {
      let el = this.$refs[newVal][0]
      this.scroll.scrollToElement(el)
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true
    })
  },
  activated () {
    this.goTop()
  }
}
</script>
<style lang="stylus" scoped>
    .border-topbottom
        &:before
            border-color #ccc
        &:after
            border-color #ccc
    .border-bottom
        &:after
            border-color #ccc
    .list
        overflow hidden
        position absolute
        top 1.58rem
        bottom 0
        left 0
        right 0
        .title
            line-height .54rem
            background-color #eee
            padding-left .2rem
            color #666
            font-size .26rem
        .buttonList
            padding .1rem .6rem .1rem .1rem
            overflow hidden
            .button-wrapper
                width 33.33%
                float left
                .button
                    margin .1rem
                    padding .1rem 0
                    border .02rem solid #ccc
                    border-radius .06rem
                    text-align center
        .item
            line-height .76rem
            padding-left .2rem
</style>
